﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;

namespace UtilPublic.Timer
{
   
    /// <summary>
    /// 自定义定时器系统
    /// </summary>
    public static class ServerTimer
    {
        /// <summary>
        /// 定时器ID序列
        /// </summary>
        private static long TimeId = 0;
        /// <summary>
        /// 豪秒定时器列表
        /// </summary>
        private static ConcurrentDictionary<long, MyTimer> TimersMillisecond = new ConcurrentDictionary<long, MyTimer>();
        /// <summary>
        /// 秒定时器列表
        /// </summary>
        private static ConcurrentDictionary<long, MyTimer> TimersSecond = new ConcurrentDictionary<long, MyTimer>();
        /// <summary>
        /// 分钟定时器列表
        /// </summary>
        private static ConcurrentDictionary<long, MyTimer> TimersMinute = new ConcurrentDictionary<long, MyTimer>();
        /// <summary>
        /// 获取ID
        /// </summary>
        public static long CreateId { get { TimeId++; return TimeId; } }

        /// <summary>
        /// 立即开始一个豪秒定时器
        /// </summary>
        /// <param name="millisecond"></param>
        /// <param name="action"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static MyTimer StartCalloutMillisecond(int millisecond, Action<object[]> action, params object[] args)
        {
            var timer = new MyTimer();
            timer.Type = ETimerType.Millisecond;
            timer.Interval = millisecond;
            timer.TimerAction += action;
            timer.ActionArgs = args;
            timer.SetTimer();
            timer.Start();
            return timer;
        }
        /// <summary>
        /// 立即开始一个秒定时器
        /// </summary>
        /// <param name="second"></param>
        /// <param name="action"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static MyTimer StartCalloutSecond(int second, Action<object[]> action, params object[] args)
        {
            var timer = new MyTimer();
            timer.Type = ETimerType.Second;
            timer.Interval = second;
            timer.TimerAction += action;
            timer.ActionArgs = args;
            timer.SetTimer();
            timer.Start();
            return timer;
        }
        /// <summary>
        /// 立即开始一个分钟定时器
        /// </summary>
        /// <param name="minute"></param>
        /// <param name="action"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static MyTimer StartCalloutMinute(int minute, Action<object[]> action, params object[] args)
        {
            var timer = new MyTimer();
            timer.Type = ETimerType.Minute;
            timer.Interval = minute;
            timer.TimerAction += action;
            timer.ActionArgs = args;
            timer.SetTimer();
            timer.Start();
            return timer;
        }
        /// <summary>
        /// 增加一个定时器
        /// </summary>
        /// <param name="timer"></param>
        /// <returns></returns>
        public static long TimerStart(MyTimer timer)
        {
            if (timer.Type == ETimerType.Second)
            {
                TimersSecond[timer.Id] = timer;
            }
            else if (timer.Type == ETimerType.Minute)
            {
                TimersMinute[timer.Id] = timer;
            }
            else
            {
                TimersMillisecond[timer.Id] = timer;
            }
            return timer.Id;
        }
        /// <summary>
        /// 删除一个定时器
        /// </summary>
        /// <param name="id"></param>
        public static void TimerStop(long id)
        {
            TimersMillisecond.TryRemove(id, out _);
            TimersSecond.TryRemove(id, out _);
            TimersMinute.TryRemove(id, out _);
        }
        /// <summary>
        /// 定时器管理器毫秒心跳
        /// </summary>
        public static void LoopMillisecond()
        {
            var dtNow = DateTime.Now;
            ArrayList timersExpired = new ArrayList();
            foreach (var kvp in TimersMillisecond)
            {
                var timer = kvp.Value;
                if (timer.Amount > 0)
                {
                    if (timer.ExecuteTime <= dtNow)
                    {
                        timer.ExecuteTimer();
                        timer.SetTimer();
                        if (!timer.AutoLoop)
                        {
                            timer.Amount--;
                            if (timer.Amount < 1)
                            {
                                timersExpired.Add(kvp.Key);
                            }
                        }
                    }
                }
                else
                {
                    timersExpired.Add(kvp.Key);
                }
            }
            foreach (long k in timersExpired)
            {
                TimersMillisecond.TryRemove(k, out _);
            }
        }
        /// <summary>
        /// 定时器管理器秒心跳
        /// </summary>
        public static void LoopSecond()
        {
            var dtNow = DateTime.Now;
            ArrayList timersExpired = new ArrayList();
            foreach (var kvp in TimersSecond)
            {
                var timer = kvp.Value;
                if (timer.Amount > 0)
                {
                    if (timer.ExecuteTime <= dtNow)
                    {
                        timer.ExecuteTimer();
                        timer.SetTimer();
                        if (!timer.AutoLoop)
                        {
                            timer.Amount--;
                            if (timer.Amount < 1)
                            {
                                timersExpired.Add(kvp.Key);
                            }
                        }
                    }
                }
                else
                {
                    timersExpired.Add(kvp.Key);
                }
            }
            foreach (long k in timersExpired)
            {
                TimersSecond.TryRemove(k, out _);
            }
        }
        /// <summary>
        /// 定时器管理器分钟心跳
        /// </summary>
        public static void LoopMinute()
        {
            var dtNow = DateTime.Now;
            ArrayList timersExpired = new ArrayList();
            foreach (var kvp in TimersMinute)
            {
                var timer = kvp.Value;
                if (timer.Amount > 0)
                {
                    if (timer.ExecuteTime <= dtNow)
                    {
                        timer.ExecuteTimer();
                        timer.SetTimer();
                        if (!timer.AutoLoop)
                        {
                            timer.Amount--;
                            if (timer.Amount < 1)
                            {
                                timersExpired.Add(kvp.Key);
                            }
                        }
                    }
                }
                else
                {
                    timersExpired.Add(kvp.Key);
                }
            }
            foreach (long k in timersExpired)
            {
                TimersMinute.TryRemove(k, out _);
            }
        }
    }
}